package com.cskaoyan.service.user.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MallOrderGoodsMapper;
import com.cskaoyan.mapper.MallOrderMapper;
import com.cskaoyan.model.vo.user.wx.index.MallOrderAndOrderGoodsById;
import com.cskaoyan.model.vo.user.wx.index.WxUserIndexData;
import com.cskaoyan.model.vo.user.wx.index.WxUserIndexOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @program: project-market
 * @description: wx端UserService实现
 * @author: Jesson Guo
 * @create: 2021-12-02 22:34
 **/
@Service
public class WxUserServiceImpl implements WxUserService {
    @Autowired
    MallOrderMapper orderMapper;
    @Autowired
    MallOrderGoodsMapper orderGoodsMapper;
  /*  @Override
    public WxUserIndexData getIndex(Integer id) {
        if (id==null) return null;
        MallOrderExample orderExample = new MallOrderExample();
        MallOrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
        orderExampleCriteria.andUserIdEqualTo(id);
        orderExampleCriteria.andDeletedEqualTo(false);
        List<MallOrder> orderList = orderMapper.selectByExample(orderExample);
        int unrecv=0;   //未收获
        int uncomment=0;    //未评论
        int unpaid=0;   //未支付
        int unship=0;   //未发货
        for (MallOrder order : orderList) {
            Short orderStatus = order.getOrderStatus();
            if (orderStatus==101)++unpaid;
            if (orderStatus==201)++unship;
            if (orderStatus==301)++unrecv;
            if (orderStatus==401||orderStatus==402){
                MallOrderGoodsExample orderGoodsExample = new MallOrderGoodsExample();
                MallOrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
                orderGoodsExampleCriteria.andDeletedEqualTo(false);
                orderGoodsExampleCriteria.andOrderIdEqualTo(order.getId());
                orderGoodsExampleCriteria.andCommentEqualTo(0);
//                uncomment=orderGoodsMapper.countByExample(orderGoodsExample)==1?++uncomment:uncomment;
                long l=orderGoodsMapper.countByExample(orderGoodsExample);
                if (l==1l){
                    uncomment++;
                }
            }

        }
        return new WxUserIndexData(new WxUserIndexOrderVO(unrecv,uncomment,unpaid,unship));
    }*/

    /**
     * order_statu  101 未支付    201 未发货  301 未收,401,402用户收货 未评价
     *
     * @param id
     * @return
     */
    @Override
    public WxUserIndexData getIndex(Integer id) {
        if (id == null) return null;
        //连接查询查出关于用户id再order表跟ordeGoods中的数据
        List<MallOrderAndOrderGoodsById> list = orderMapper.getIndex2(id);
        //分别创建四种条件
//      Predicate<MallOrderAndOrderGoodsById> P1=s->s.getOrderStatus()==101;
//      Predicate<MallOrderAndOrderGoodsById> P2=s->s.getOrderStatus()==201;
        Predicate<MallOrderAndOrderGoodsById> P3 = s -> s.getOrderStatus() == 301;
        Predicate<MallOrderAndOrderGoodsById> P4 = s -> s.getOrderStatus() == 401 || s.getOrderStatus() == 402;
        //订单的comment状态是否为0
        Predicate<MallOrderAndOrderGoodsById> P5 = s -> s.getComments() > 0;
        //状态为101的订单list 未付款
        List<MallOrderAndOrderGoodsById> collect1 = list.stream().filter(s -> s.getOrderStatus() == 101).collect(Collectors.toList());
        //状态为201的订单list 未发货
        List<MallOrderAndOrderGoodsById> collect2 = list.stream().filter(s -> s.getOrderStatus() == 201).collect(Collectors.toList());
        //状态为301的订单list 未收货
        List<MallOrderAndOrderGoodsById> collect3 = list.stream().filter(P3).collect(Collectors.toList());
        //状态为401和402未评论的订单list 未评价
        //过滤条件可以选择and也可以选择or  ,前提是要是Predicate对象才能用
        List<MallOrderAndOrderGoodsById> collect4 = list.stream().filter(P4.and(P5)).collect(Collectors.toList());
        return new WxUserIndexData(new WxUserIndexOrderVO(collect3.size(), collect4.size(), collect1.size(), collect2.size()));

    }
}
